package com.lagou.edu.controller;

import com.lagou.edu.callback.Callback;
import com.lagou.edu.callback.JsonData;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @Author: hwq
 * @Date: 2020/10/22 10:14
 * @Version 1.0
 */
@Controller
@RequestMapping("/login")
public class LoginController {
    @PostMapping("/login")
    @ResponseBody
    public JsonData login(String username, String password, HttpServletRequest request) {

        if ("admin".equals(username) && "admin".equals(password)) {
            HttpSession session = request.getSession();
            session.setAttribute("user", "admin");
            return Callback.success("success", "查询成功", null);

        } else {
            return Callback.fail("success", "查询成功", null);
        }


    }

    @GetMapping("/login")
    public ModelAndView list(Model model,String msg) {
if(msg==null){
    msg="login";
}
        ModelAndView modelAndView = new ModelAndView("/login");
        modelAndView.addObject("msg", msg);
        return modelAndView;
    }
}
